package com.willian.andrappfw.utils;

import android.content.Context;
import android.os.Build;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.berwin.cocoadialog.CocoaDialog;
import com.bumptech.glide.Glide;
import com.willian.andrappfw.R;
import com.willian.andrappfw.ui.WCustomDialogBuilder;


public class DialogUtils {
    public static CocoaDialog createLoadingDialog(Context context) {
        return WCustomDialogBuilder.with(context)
                .setLayoutId(R.layout.dialog_loading_gif)
                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setInitListener((dialog, view) ->
                        Glide.with(context).load(R.drawable.icon_loading).into((ImageView) view.findViewById(R.id.imageView_loadGif))
                )
                .create();
    }

//    public static CocoaDialog createTransparentLoadingDialog(Context context) {
//        return SocoCustomDialogBuilder.with(context)
//                .setLayoutId(R.layout.dialog_loading_gif)
//                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
//                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
//                .setInitListener((dialog, view) -> {
//                            view.setBackgroundColor(Color.TRANSPARENT);
//                            Glide.with(context).load(R.drawable.icon_loading).into((ImageView) view.findViewById(R.id.imageView_loadGif));
//                        }
//                )
//                .create();
//    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static CocoaDialog createResultDialog(Context context, boolean successful, final String message) {
        return WCustomDialogBuilder.with(context)
                .setLayoutId(R.layout.dialogue_result)
                .setWidth(WindowManager.LayoutParams.MATCH_PARENT)
                .setHeight(WindowManager.LayoutParams.MATCH_PARENT)
                .setOnClickListener((cocoaDialog, id) -> {
                    if (id == R.id.click_bglayout) {
                        WDialogBuilder.dismissDialog(cocoaDialog);
                    }
                })
                .setClickableViews(R.id.click_bglayout)
                .setInitListener((dialog, view) -> {
                    ((ImageView) view.findViewById(R.id.imageView_tip)).setImageResource(successful ? R.drawable.icon_success : R.drawable.icon_failed);
                    TextView textView = view.findViewById(R.id.textView_tip);
                    textView.setText(message);
                    textView.setTextColor(context.getColor(successful ? R.color.green_01BC67 : R.color.red_FF2D55));
                    dialog.setOnShowListener(dialog1 -> {
                        view.postDelayed(() -> WDialogBuilder.dismissDialog(dialog), 3000);
                    });
                })
                .create();
    }

}
